package org.consoleTesting;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class LoginSystem {
	private final static String SPLITTER = "/";
	private final static String AUTH_FILE = "users";

	public static User userLogin(String login) {
		Scanner file_scan = null;
		try {
			file_scan = new Scanner(new FileReader(AUTH_FILE));
		} catch (FileNotFoundException e) {
			System.err.println("Error oprning file users, exiting");
			System.exit(0);
		}
		while (file_scan.hasNext()) {
			String f_out = file_scan.nextLine();
			String[] buf = f_out.split(SPLITTER);
			if (buf[1].equals(login)) {
				if (buf[2].equals("admin")) {
					return new User(login, true);
				} else {
					return new User(login, false);
				}
			}
		}
		return null;
	}

	public static void singUp(String login) {
		Integer id = Integer.parseInt(readLastLine(AUTH_FILE).split(SPLITTER)[0]) + 1;
		FileWriter wr;
		try {
			wr = new FileWriter(AUTH_FILE, true);
			wr.write(String.format("%d/%s/student%n", id, login));
			wr.close();
		} catch (IOException e) {
			System.err.println("can't write to file for auth");
		}
		
	}

	public static String getUserName(Integer userId) {

		Scanner file_scan = null;
		try {
			file_scan = new Scanner(new FileReader(AUTH_FILE));
		} catch (FileNotFoundException e) {
			System.err.println("something wrong with users file, exiting");
			System.exit(0);

		}
		while (file_scan.hasNext()) {
			String f_out = file_scan.nextLine();
			String[] buf = f_out.split(SPLITTER);
			if (buf[0].equals(userId.toString())) {
				return buf[1];
			}
		}
		return null;
	}

	private static String readLastLine(String fileName) {
		try {
			java.io.File file = new java.io.File(fileName);
			java.io.RandomAccessFile fileHandler = new java.io.RandomAccessFile(
					file, "r");
			long fileLength = file.length() - 1;
			StringBuilder sb = new StringBuilder();

			for (long filePointer = fileLength; filePointer != -1; filePointer--) {
				fileHandler.seek(filePointer);
				int readByte = fileHandler.readByte();

				if (readByte == 0xA) {
					if (filePointer == fileLength) {
						continue;
					} else {
						break;
					}
				} else if (readByte == 0xD) {
					if (filePointer == fileLength - 1) {
						continue;
					} else {
						break;
					}
				}

				sb.append((char) readByte);
			}
			fileHandler.close();
			String lastLine = sb.reverse().toString();
			return lastLine;
		} catch (java.io.FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (java.io.IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String allUsersForDisplay() {
		StringBuilder UsersForDisplay = new StringBuilder();
		Scanner file_scan = null;
		try {
			file_scan = new Scanner(new FileReader(AUTH_FILE));
		} catch (FileNotFoundException e) {
			System.err.println("something wrong with users file, exiting");
			System.exit(0);

		}
		while (file_scan.hasNext()) {
			String[] buf = file_scan.nextLine().split(SPLITTER);
			;
			if (buf[2].equals("admin")) {
				continue;
			} else {
				UsersForDisplay.append(buf[0]).append(" ").append(buf[1])
						.append("\n");
			}

		}

		return UsersForDisplay.toString();
	}
}
